package entity

import (
	"fmt"
	"strconv"
	"time"

	"gorm.io/gorm"
	"shqsoft.cn/pas3ui/base"
)

//ShCompClieQygm 对公企业规模维护
type ShCompClieQygm struct {
	ID   int    `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	ENCD string `gorm:"column:ENCD"`
	CUNM string `gorm:"column:CUNM"`
	QYGM string `gorm:"column:QYGM"`
	DRYF int    `gorm:"column:DRYF"`
	CUID int    `gorm:"column:CUID"`
}

//TableName
func (ShCompClieQygm) TableName() string {
	return "SH_COMP_CLIE_QYGM"
}

// BeforeCreate
func (u *ShCompClieQygm) BeforeCreate(tx *gorm.DB) (err error) {
	var cuid *int
	base.CHDB.Raw(`SELECT "CUID" FROM "T_COMP_CLIE" WHERE "ENCD"=?`, u.ENCD).Row().Scan(&cuid)
	if cuid == nil {
		base.CHDB.Raw(`SELECT "CUID" FROM "T_COMP_CLIE" WHERE "CUNM"=?`, u.CUNM).Row().Scan(&cuid)
	}
	if cuid == nil {
		err = fmt.Errorf("组织机构代码%s或客户名称%s在系统中不存在！", u.ENCD, u.CUNM)
		return
	}
	u.CUID = *cuid
	//判断是否重复导入
	var count int
	if u.ID == 0 {
		tx.Raw(`SELECT COUNT(*) FROM "SH_COMP_CLIE_QYGM" WHERE "CUID"=? AND "DRYF"=?`, u.CUID, u.DRYF).Row().Scan(&count)
		if count > 0 {
			err = fmt.Errorf("客户名称:%s,不能重复导入！", u.CUNM)
			return
		}
	}
	if u.QYGM != "大型" && u.QYGM != "中型" && u.QYGM != "小型" && u.QYGM != "微型" {
		err = fmt.Errorf("企业规模%s不正确！", u.QYGM)
		return
	}
	return
}

// AfterCreate
func (u *ShCompClieQygm) AfterCreate(tx *gorm.DB) (err error) {
	var count int
	var sqlStr, dtstr string
	var monthStartTime *time.Time
	var dtid time.Time
	dtstr = strconv.Itoa(u.DRYF)
	dtstr = dtstr[0:4] + "-" + dtstr[4:6] + "-01"
	dtid, _ = time.ParseInLocation("2006-01-02", dtstr, time.Local)
	edtid, _ := time.Parse("2006-01-02", "2099-12-31")
	tx.Raw(`SELECT COUNT(*) FROM "SH_COMP_CLIE_QYGM_ZIPPER" WHERE "CUID"=? AND "EDATE"=?`, u.CUID, edtid).Row().Scan(&count)
	if count == 0 {
		monthStartTime = base.GetFirstDateOfMonth(&dtid)
		sqlStr = `INSERT INTO "SH_COMP_CLIE_QYGM_ZIPPER"("CUID", "SDATE", "EDATE", "QYGM") VALUES(?, ?, ?, ?)`
		err = tx.Exec(sqlStr, u.CUID, monthStartTime, edtid, u.QYGM).Error
		if err != nil {
			return err
		}
	}
	return
}

func ShCompClieQygmAfterImport(tx *gorm.DB, params map[string]string) (err error) {
	prams := params["DRYF"]
	dtstr := prams[0:4] + "-" + prams[4:6] + "-01"
	dtid, _ := time.Parse("2006-01-02", dtstr)
	dtid = dtid.AddDate(0, -1, 0)
	monthEndTime := base.GetLastDateOfMonth(&dtid)
	var sqlStr string
	if base.AppConf.Database.Mysql != "" {
		sqlStr = `UPDATE SH_COMP_CLIE_QYGM_ZIPPER A,SH_COMP_CLIE_QYGM B
		SET A.EDATE=?
		WHERE A.CUID=B.CUID AND B.CUID IS NULL AND A.EDATE='2099-12-31'`
	} else {
		sqlStr = `UPDATE "SH_COMP_CLIE_QYGM_ZIPPER" A SET "EDATE"=?
		FROM "SH_COMP_CLIE_QYGM" B
	  WHERE A."CUID"=B."CUID" AND B."CUID" IS NULL AND A."EDATE"='2099-12-31'`
	}
	err = tx.Exec(sqlStr, monthEndTime).Error
	if err != nil {
		return err
	}
	tx.Commit()
	base.CHDB.Exec("system reload dictionary pas_olap_nj.DICT_COMP_CLIE_QYGM_ZIPPER")
	return
}

//ShCompClieQygmEF 返回结构体
func ShCompClieQygmEF() base.EntityFunc {
	return func() interface{} {
		entity := ShCompClieQygm{}
		return &entity
	}
}

//ShCompClieQygmESF 返回结构体数组
func ShCompClieQygmESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []ShCompClieQygm
		return &entities
	}
}

func init() {
	base.SetAfterCallBack("ShCompClieQygm", ShCompClieQygmAfterImport)
	base.DaoPoolObj.Put("ShCompClieQygm", base.NewDefaultDao(ShCompClieQygmEF(), ShCompClieQygmESF(), nil, "my"))
}
